package com._51cto.healthy.controller;


import com._51cto.healthy.content.P;
import com._51cto.healthy.content.R;
import com._51cto.healthy.pojo.CheckGroup;
import com._51cto.healthy.service.CheckGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Api(value = "检测组管理" , tags = "检测组管理")
@CrossOrigin
@RestController
@RequestMapping("/check-group")
public class CheckGroupController {

    @DubboReference
    private CheckGroupService checkGroupService;

    @ApiOperation(value = "分页查询", notes = "检测组信息-分页获取", tags = "检测组管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query",value = "名称or编码or助记码",dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageSize",value = "每页数据",defaultValue = "10",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum",value = "当前页数",defaultValue = "1",dataTypeClass = Integer.class)
    })
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @GetMapping("/page/{pageSize}/{pageNum}")
    public R queryAll(@RequestParam(value = "query",required = false/*表示参数不是必填项*/) String query,
                      @PathVariable("pageNum") Integer pageNum,
                      @PathVariable("pageSize") Integer pageSize){
        P<CheckGroup> checkGroupP = checkGroupService.queryPage(query, pageNum, pageSize);
        return R.success(checkGroupP);
    }


    @ApiOperation(value = "检测项Ids", notes = "检测组与检测项关联信息Ids", tags = "检测组管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "检测组数据主键",dataTypeClass = Integer.class),
    })
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    @GetMapping("/item/{id}")
    public R queryItemIds(@PathVariable("id") Integer id){
        List<Integer> idS = checkGroupService.queryItemIds(id);
        return R.success(idS);
    }

    @ApiOperation(value = "检测组删除", notes = "检测组信息-主键删除，级联删除检测项关联信息", tags = "检测组管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "检测组数据主键", dataTypeClass = Integer.class),
    })
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    @DeleteMapping("/{id}")
    public R deleteByPk(@PathVariable("id") Integer pk){
        checkGroupService.deleteByPK(pk);
        return R.success("删除成功");
    }


    @ApiOperation(value = "检测组新增", notes = "新增检测组信息and检测项关联信息", tags = "检测组管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "checkGroup",value = "检测组数据Json/包括关联检测项ids",dataTypeClass=CheckGroup.class),
    })
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    @PostMapping()
    public R insert(@RequestBody CheckGroup checkGroup){
        checkGroupService.insert(checkGroup);
        return R.success("添加成功");
    }


    @ApiOperation(value = "检测组编辑", notes = "编辑检测组信息and检测项关联信息", tags = "检测组管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "checkGroup",value = "检测组数据Json/包括关联检测项ids",dataTypeClass=CheckGroup.class),
    })
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    @PutMapping()
    public R update(@RequestBody CheckGroup checkGroup){
        checkGroupService.update(checkGroup);
        return R.success("更新成功");
    }
}
